﻿/*
 *  Copyright (C) 2010 by Benjamin Nolmans <benjamin.nolmans@gmail.com>
 *  and Barbara Post <postb99@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using OsamesFrameworkCore.Plugins;
using OsamesFrameworkCore.Logging;


namespace HelloWorldPlugin
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:HelloWorldCustomControl runat=server></{0}:HelloWorldCustomControl>")]
    public class HelloWorldCustomControl : WebControl
    {

        #region Attributs
        // Label Component
        private Label _helloworldLabel; 
        #endregion     

        #region Properties
        public override ControlCollection Controls
        {
            get
            {
                EnsureChildControls();
                return base.Controls;
            }
        }

        public IPluginHost PluginHost { get; set; }

        public IPlugin Plugin { get; set; }

        #endregion

        #region Constructor
        /// <summary>
        /// Constructor
        /// </summary>
        public HelloWorldCustomControl()
        {
            // TODO [web plugin internals / plugin hello world] pass Plugin as constructor argument to have a reference as soon as control is created by plugin.

            Plugin = null;
            PluginHost = null;
        }
        #endregion

        #region Public Methods
        /// <summary>
        /// Calls protected overriden CreateChildControl() to initialize the web control
        /// </summary>
        public void InitializeControl()
        {
            Plugin.Log(LogLevel.Trace, "InitializeControl");
            CreateChildControls();
        }

        /// <summary>
        /// Affects data to the control
        /// </summary>
        /// <returns></returns>
        public void InitializeHelloWorldLabelData()
        {
            Plugin.Log(LogLevel.Trace, "InitializeHelloWorldLabelData");

            _helloworldLabel = new Label()
            {
                ID = "helloworldlabel",
                Text = "Hello World",
            };
        } 
        #endregion

        #region Protected Overriden Methods
        protected override void CreateChildControls()
        {
            Plugin.Log(LogLevel.Trace, "CreateChildControls");            

            Controls.Clear();

            //create & add Label control
            InitializeHelloWorldLabelData();
            //add the control to the tree
            Controls.Add(_helloworldLabel);

            base.CreateChildControls();
        }

        protected override void Render(HtmlTextWriter output_)
        {
            Plugin.Log(LogLevel.Trace, "Render");

            //create the render data of the web control
            _helloworldLabel.RenderControl(output_);
        }
        #endregion
    }
}
